package com.yanqu.road.dao.impl.activity.tonbing.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.tonbing.config.TonBingNpcEventConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TonBingNpcEventConfigDaoImpl extends TempDao {

    private static String getTonBingNpcEventConfigSql = "select * from `t_s_activity_peaktongbing_event` where `activityId`=?";

    public Map<Integer, TonBingNpcEventConfig> getTonBingNpcEventConfigMap(int activityId) {
        Map<Integer, TonBingNpcEventConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getTonBingNpcEventConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                TonBingNpcEventConfig data = new TonBingNpcEventConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setWeight(rs.getInt("weight"));
                data.setIcon(rs.getString("icon"));
                data.setRewards(rs.getString("rewards"));
                // 缓存
                data.setRewardList(StringUtils.stringToStringList(data.getRewards(), "\\|"));
                map.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
